package com.boc.ewr.business.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boc.ewr.bcimp.entity.CustomDepart;
import com.boc.ewr.bcimp.service.ICustomDepartService;
import com.boc.ewr.business.entity.EwrManageExam;
import com.boc.ewr.business.entity.EwrReceiptRatio;
import com.boc.ewr.business.mapper.EwrManageExamMapper;
import com.boc.ewr.business.service.IEwrManageExamService;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 智慧e对-对账管理考核
 * @Author: jeecg-boot
 * @Date:   2023-06-08
 * @Version: V1.0
 */
@Service
public class EwrManageExamServiceImpl extends ServiceImpl<EwrManageExamMapper, EwrManageExam> implements IEwrManageExamService {

    @Autowired
    ICustomDepartService customDepartService;

    @Autowired
    EwrManageExamMapper ewrManageExamMapper;

    @Override
    public boolean createIEMInfo(String caMon) {

        //条线履职-风险核查反馈
        List<EwrManageExam> riskInfo = ewrManageExamMapper.createRiskCInfo(caMon);
        List<EwrManageExam> questOne = ewrManageExamMapper.createQuestOneInfo();
        List<EwrManageExam> questTwe = ewrManageExamMapper.createQuestTwoInfo();
        List<EwrManageExam> questThr = ewrManageExamMapper.createQuestThrInfo();
        List<EwrManageExam> checkInfo = ewrManageExamMapper.createCheckInfo(caMon);
        List<EwrManageExam> exterInfo = ewrManageExamMapper.createExterInfo(caMon);
        List<EwrManageExam> innerInfo = ewrManageExamMapper.createInnerInfo(caMon);
        //数据汇总
        Map<String, EwrManageExam> maps = new HashMap<>();
        handleList(riskInfo, maps);
        handleList(questOne, maps);
        handleList(questTwe, maps);
        handleList(questThr, maps);
        handleList(checkInfo, maps);
        handleList(exterInfo, maps);
        handleList(innerInfo, maps);
        //补充不完整的数据（管辖机构号\管辖机构名称\签约机构名称）
        List<EwrManageExam> branchList = new ArrayList<>(maps.values());
        supplementList(branchList);
        //清除之前生成的数据
        ewrManageExamMapper.delete(null);
        return saveBatch(branchList);
    }

    /**
     * 给数据汇总合并
     * @param Info
     * @param map
     */
    private void handleList(List<EwrManageExam> Info, Map<String, EwrManageExam> map){
        for(EwrManageExam inf : Info){
            if(map.get(inf.getBranchNo()) != null){
                EwrManageExam ratio = map.get(inf.getBranchNo());
                //风险核查反馈问题数
                if(StringUtils.isNotEmpty(inf.getRiskFeebackNum())){
                    int tempa = StringUtils.isEmpty(ratio.getRiskFeebackNum()) ? 0 : Integer.parseInt(ratio.getRiskFeebackNum());
                    int tempb = Integer.parseInt(inf.getRiskFeebackNum());
                    int num = tempa + tempb;
                    ratio.setRiskFeebackNum(String.valueOf(num));
                }
                //整改问题数
                if(StringUtils.isNotEmpty(inf.getRectificationNum())){
                    int tempa = StringUtils.isEmpty(ratio.getRectificationNum()) ? 0 : Integer.parseInt(ratio.getRectificationNum());
                    int tempb = Integer.parseInt(inf.getRectificationNum());
                    int num = tempa + tempb;
                    ratio.setRectificationNum(String.valueOf(num));
                }
                //省行抽检问题提交数
                if(StringUtils.isNotEmpty(inf.getProInspectionNum())){
                    int tempa = StringUtils.isEmpty(ratio.getProInspectionNum()) ? 0 : Integer.parseInt(ratio.getProInspectionNum());
                    int tempb = Integer.parseInt(inf.getProInspectionNum());
                    int num = tempa + tempb;
                    ratio.setProInspectionNum(String.valueOf(num));
                }
                //网银渠道对账代客对账问题数
                if(StringUtils.isNotEmpty(inf.getValetRecNum())){
                    int tempa = StringUtils.isEmpty(ratio.getValetRecNum()) ? 0 : Integer.parseInt(ratio.getValetRecNum());
                    int tempb = Integer.parseInt(inf.getValetRecNum());
                    int num = tempa + tempb;
                    ratio.setValetRecNum(String.valueOf(num));
                }
                //对账年月\管辖机构号\管辖机构名称\签约机构名称
                if(StringUtils.isNotEmpty(inf.getCaMon())) ratio.setCaMon(inf.getCaMon());
                if(StringUtils.isNotEmpty(inf.getParentBranchNm())) ratio.setParentBranchNm(inf.getParentBranchNm());
                if(StringUtils.isNotEmpty(inf.getBranchNm())) ratio.setBranchNm(inf.getBranchNm());

            }else {
                //新增
                EwrManageExam ratio = new EwrManageExam();
                BeanUtils.copyProperties(inf, ratio);
                map.put(inf.getBranchNo(), ratio);
            }
        }
    }

    /**
     * 补充对应的管辖机构和名称
     * @param Info
     */
    private void supplementList(List<EwrManageExam> Info){
        for(EwrManageExam inf : Info){
            if(StringUtils.isEmpty(inf.getParentBranchNm())){
                CustomDepart departOne = customDepartService.getById(inf.getBranchNo());
                inf.setBranchNm(departOne.getDepartName());
                inf.setParentBranchNm(departOne.getDepartLeader());
            }

        }
    }

}
